<template>
	<view class="indent_content">
		<view class="canteen">
			<image src="@/static/logo.png"></image>
			<view class="org_name">
				益而益集团餐厅 - {{indent.region}}
			</view>
			<view class="status" @click="callPhone">
				<van-icon name="phone" />
			</view>
		</view>
		<view class="sub_title">商品明细</view>
		<view class="goods_list">
			<block v-if="goodsList.length">
				<view class="goods" v-for="(it, i) in sliceGoodsList" :key="it._id">
					<view class="img" @click="previewImg(it.goods_thumb)">
						<van-image width="120rpx" height="120rpx" lazy-load fit="cover" :src="it.goods_thumb" />
						<van-icon name="expand-o" size="40rpx" color="#f6f6f6" />
					</view>
					<div class="content">
						<section>
							<p class="name">{{it.name}}</p>
							<p class="specification" v-if="orderFormGoods[i].specification_selected">
								{{orderFormGoods[i].specification_selected}}
							</p>
						</section>
						<p class="num">x{{orderFormGoods[i].goods_num}}</p>
						<p class="price">¥{{orderFormGoods[i].unit_price}}</p>
					</div>
				</view>
				<p 
					v-if="goodsList.length > 3 && showNum !== goodsList.length" 
					@click="showNum = goodsList.length" 
					class="more">显示全部</p>
			</block>
			<view v-else class="loaging">
				<uni-load-more status="loading"></uni-load-more>
			</view>
		</view>
		<view class="total_price">
			<p style="margin-right: 20rpx;">
				就餐人数: <span>{{indent.dining_person_num}}</span>人, 人均: <span>{{perCapita}}</span>元
			</p>
			<p class="p1">商品总额:</p>
			<p class="p2">¥{{indent.total_fee}}</p>
		</view>
	</view>
</template>

<script>
	const db = uniCloud.database()
	const goodListDB = db.collection('dish-list')
	export default {
		name: 'indent-content',
		data() {
			return {
				goodsList: [],
				showNum: 3
			}
		},
		props: ['indent'],
		computed: {
			orderFormGoods() {
				return this.indent.order_form_num['dish-order-form-goods']
			},
			sliceGoodsList() {
				return this.goodsList.slice(0, this.showNum)
			},
			perCapita() {
				return (this.indent.total_fee / this.indent.dining_person_num).toFixed(2)
			}
		},
		mounted() {
			this.requestGoods()
		},
		methods: {
			async requestGoods() {
				const {indent} = this
				const goods_id_list = this.orderFormGoods.map(e => e.goods_id)
				const goods = await goodListDB.where({'_id': db.command.in(goods_id_list)}).get().then(r => r.result.data)
				this.goodsList = goods_id_list.map(e => goods.find(v => e === v._id))
			},
			callPhone() {
				const publicInfo = this.$store.state.user.publicInfo
				const map = {
					'苏州': 'phone_suzhou',
					'温州': 'phone_wenzhou'
				}
				wx.makePhoneCall({
				  phoneNumber: publicInfo[map[this.indent.region]]
				})
			},
			previewImg(url) {
				uni.previewImage({
					current: 0,
					urls: [url]
				})
			},
		}
	}
</script>

<style lang="scss">
	.indent_content {
		margin-top: 20rpx;
		background-color: #fff;
		border-radius: 12rpx;
		padding: 0 20rpx;
		// min-height: 706rpx;
		
		.canteen {
			padding: 16rpx 0;
			height: 80rpx;
			border-bottom: 1px solid #f4f4f4;
			display: flex;
			align-items: center;
		
			>image {
				width: 144rpx;
				height: 20rpx;
			}
		
			.org_name {
				font-size: 28rpx;
				margin-left: 20rpx;
				font-weight: bold;
				color: black;
			}
		
			.status {
				margin-left: auto;
				width: 50rpx;
				height: 50rpx;
				border-radius: 50%;
				background-color: #f2f2f2;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
		
		.sub_title {
			font-size: 28rpx;
			font-weight: bold;
			padding: 20rpx 0;
		}
		
		.goods_list {
			padding-bottom: 6rpx;
			border-bottom: 1px solid #f2f2f2;
			
			.goods {
				display: flex;
				align-items: start;
				margin-bottom: 20rpx;
				
				.img {
					width: 120rpx;
					height: 120rpx;
					background: #f7f8fa;
					display: flex;
					justify-content: center;
					align-items: center;
					border-radius: 10rpx;
					overflow: hidden;
					margin-right: 15rpx;
					position: relative;
					>van-icon {
						position: absolute;
						right: 0;
						top: 0;
					}
				}
				.content {
					flex: 1;
					display: flex;
					align-items: start;
					justify-content: space-between;
					.name {
						font-size: 28rpx;
						font-weight: 500;
						width: 340rpx;
					}
					.specification {
						font-size: 24rpx;
						color: #8e939c;
						font-weight: 0;
					}
					.num {
						color: #656565;
						font-size: 26rpx;
					}
					.price {
						font-size: 26rpx;
					}
				}
			}
			
			.more {
				width: 180rpx;
				text-align: center;
				font-size: 24rpx;
				padding: 4rpx 0;
				border-radius: 30rpx;
				border: 1px solid #dce2ec;
				color: #666a71;
				margin: 0 auto 20rpx;
			}
			
			.loaging {
				flex: 1;
				height: 120rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	
		.total_price {
			height: 84rpx;
			display: flex;
			align-items: center;
			justify-content: flex-end;
			font-size: 28rpx;
			color: #3e3e3e;
			span {
				color: #F56C6C;
				margin-left: 8rpx;
			}
			.p2 {
				margin-left: 20rpx;
				font-size: 28rpx;
				color: #F56C6C;
				letter-spacing: 1px;
				font-weight: bold;
			}
		}
	}
</style>